<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-DRC-double-info-widget" :style="getStyle('head')">
      <div class="widget-inside">
        <!--科室信息-->
        <div class="dept-room" :class="getDeptClassName">
          <p class="dept" v-if="!(optionsInfo.deptHide)&&false">{{ getRoomDeptInfo.deptName || '\u00a0' }}</p>
          <p class="room" v-if="!(optionsInfo.roomHide)&&false">
            {{ getRoomDeptInfo[optionsInfo['roomNameKey']] || getRoomDeptInfo.roomName || '\u00a0' }}
          </p>
          <p class="room-merge">
            <ScrollX>{{ getDeptRoomInfo }}</ScrollX>
          </p>
        </div>
        <!--医生信息-->
        <div class="doc-room-info">
          <template v-for="(item,index) in getRoomDoctorInfo">
            <template v-if="index===1">
              <div class="divider"></div>
            </template>
            <div class="doc-info" :class="{'first': index===0,'second': index===1,}">
              <div class="right picture">
                <img :src="getDoctorPicture(item)" alt="医生">
              </div>
              <div class="left" :class="waitPatientsClass(item)">
                <div class="doc">
                  <div class="content" v-if="!!item['workerName']">
                    <ScrollX>{{ item['workerName'] || '\u00a0' }}</ScrollX>
                  </div>
                  <div class="title">
                    <ScrollX>{{ item['workerTitle'] || '\u00a0' }}</ScrollX>
                  </div>
                </div>
                <div class="patient">
                  <p class="patient-info">
                    <span class="title">就诊：</span>
                    <span class="content progress">
                      <template v-if="item&&!!item['treatingPatient']&&!!item['treatingPatient'][0]">
                        <template v-if="item['treatingPatient'][0]?.orderNo">
                          {{ item['treatingPatient'][0].orderNo || '\u00a0' }}
                        </template>
                        <template v-if="item['treatingPatient'][0]?.patientName">
                          {{ item['treatingPatient'][0].patientName || '\u00a0' }}
                        </template>
                      </template>
                  </span>
                  </p>
                  <div class="patient-info">
                    <div class="title">候诊：</div>
                    <div class="content wait">
                      <ul class="wait-inside">
                        <li v-for="(itemInfo, index) in waitPatientsList(item)" :key="index">
                          <template v-if="itemInfo?.orderNo">
                            {{ itemInfo.orderNo || '\u00a0' }}
                          </template>
                          {{ itemInfo.patientName || '\u00a0' }}
                        </li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </div>
      </div>

    </div>


  </CompOperate>
</template>

<script>
import {pxToVw} from '@/utils/common';

import emitter from "@/utils/designer/emitter";
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import {NoticeBar} from "vant";

let buildParent = buildParentApi();
export default {
  name: "scene-DRC-double-blue-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    NoticeBar,
    ScrollX,
    CompOperate,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
        }
      }
    },

    //获取科室诊室信息
    getDeptRoomInfo() {
      const roomDeptInfo = (this.getRoomDeptInfo);
      let {deptHide, roomNameKey} = (this.optionsInfo);
      let content = roomDeptInfo[roomNameKey] || roomDeptInfo['roomName'];
      if (!deptHide) {
        content = `${roomDeptInfo['deptName']}「${roomDeptInfo[roomNameKey] || roomDeptInfo['roomName']}」`
      }
      return content || '\u00a0'
    },

    // 科室隐藏
    getDeptClassName() {
      let {deptHide} = (this.optionsInfo);
      if (deptHide) {
        return 'dept-room-hide'
      }
    },

    // 设置不同模式下的样式
    getPatientClassName() {
      let {waitMode} = (this.optionsInfo)
      return function (type) {
        if (type === 'wait-patient') {
          switch (waitMode) {
            case 1:
              return 'current-patient'
            case 2:
              return 'current-patient2'
            case 3:
              return 'current-patient3'
          }
        } else if (type === 'wait-patient') {
          switch (waitMode) {
            case 1:
              return 'wait-patient'
            case 2:
              return 'wait-patient2'
            case 3:
              return 'wait-patient3'
          }
        }
      }
    },

    getDoctorClassName() {
      let {deptHide, roomHide} = (this.optionsInfo);
      if (deptHide && roomHide) {
        return 'doctor-info-2'
      } else if (deptHide || roomHide) {
        return 'doctor-info-1'
      } else {
        return 'doctor-info'
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDeptInfo'];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let temp = Array();
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      if (devType) {
        if (!!(roomDoctorInfo.apiData?.length)) {
          if ((roomDoctorInfo.apiData.length) >= 2) {
            return roomDoctorInfo.apiData.slice(0, 2);
          } else {
            let arr = (new Array(2 - roomDoctorInfo.apiData.length).fill({}));
            return [...roomDoctorInfo.apiData, ...arr];
          }
        } else {
          return temp.push({}, {});
        }
      } else {
        if (!!(roomDoctorInfo.mockData?.length)) {
          if ((roomDoctorInfo.mockData.length) >= 2) {
            return roomDoctorInfo.mockData.slice(0, 2);
          } else {
            let arr = (new Array(2 - roomDoctorInfo.mockData.length).fill({}));
            return [...roomDoctorInfo.mockData, ...arr];
          }
        } else {
          return temp.push({}, {});
        }
      }
    },

    // 获取医生头像
    getDoctorPicture() {
      return function (row) {
        let isPause = (row?.isPause);
        let header = '/tpleditor/resource/assets/images/header.png';
        if (!isPause) {
          return (row.workerPicture) || header
        } else {
          return this.pause;
        }
      }
    },

    //候诊人数判别:设置的人数和诊室候诊人户都要考虑到
    waitPatientsClass() {
      return function (val) {
        let waitNum = this.optionsInfo.waitMode || 1;
        switch (waitNum) {
          case 1:
            return `patient-1`;
          case 2:
            let waitPatientsList = this.waitPatientsList(val);
            if (waitPatientsList && waitPatientsList.length === 2) {
              return `patient-2`;
            } else {
              return `patient-1`;
            }
        }
      };
    },

    // 获取等待就诊的医生列表信息
    waitPatientsList() {
      return function (val) {
        let waitingPatient = val.waitingPatientList;
        if (Array.isArray(waitingPatient)) {
          let waitNum = this.optionsInfo.waitMode || 1;
          switch (waitNum) {
            case 1:
            case 2:
              return waitingPatient.slice(0, waitNum);
            default:
              return waitingPatient.slice(0, 1);
          }
        } else {
          return [];
        }
      };
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          console.log(`科室信息-${data}`)
          this.states = true;
          //buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-科室信息', key: key, data: data}))
          this.$store.dispatch('setRoomDeptInfo', data)
          break
        case "roomDoctorInfo":
          console.log(`全量医患信息-${data}`)
          this.states = true;
          // this.parentApi.executeParentMethod('writeLog', JSON.stringify({sign:'模板日志-医生信息',key:key,data:data}));
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
      }
    },
    // 滚动结束
    handleScrollEnd() {
    },
  },

  watch: {
    field: {
      handler: function (val) {
        let key = (val.options.config)
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-DRC-double-info-widget {
  padding: 28px 30px;
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  color: #333;
  height: 150px;
  font-size: 40px;
  background: transparent;
  overflow: hidden;


  .widget-inside {
    height: calc(100%);
    border-radius: 42px 42px 16px 16px;
    overflow: hidden;
    background: rgba(0, 0, 0, 0.3);

    //科室
    .dept-room {
      padding: 20px 0 8px 0;
      background: rgba(0, 0, 0, 0.3);
      height: 200px;
      overflow: hidden;

      p {
        color: #FFFFFF;
        text-align: center;
        font-size: 80px;
        padding: 0 30px;
      }

      .dept {
        font-size: 80px;
        line-height: 120px;
        @include singleEllipsis(100%)
      }

      .room {
        font-size: 120px;
        font-weight: normal;
        line-height: 180px;
      }

      .room-merge {
        font-size: 120px;
        height: 100%;
        font-weight: normal;
        line-height: 180px;
      }
    }

    //科室隐藏
    .dept-room-hide {
      display: flex;

      p {
        width: 100%;
        align-self: center;
      }
    }

    //医生信息
    .doc-room-info {
      padding: 10px 46px;

      .doc-info {
        display: flex;

        .picture {
          flex-basis: 323px;
          height: 438px;
          border-radius: 16px;
          overflow: hidden;
          margin-right: 40px;

          img {
            width: 100%;
            height: 100%;
          }
        }

        .left {
          color: white;
          overflow: hidden;
          flex-basis: calc(100% - 323px);

          .doc {
            display: flex;
            flex-wrap: nowrap;
            margin-top: 24px;
            box-sizing: border-box;
            min-height: 112px;

            .title {
              font-size: 48px;
              font-weight: 400;
              display: flex;
              flex-shrink: 0;
              width: 180px;
              color: #ffffff;
              line-height: 75px;
              align-self: center;
              align-items: center;
              margin-left: 20px;
            }

            .content {
              background: #0f2032;
              border-radius: 16px;
              font-size: 66px;
              align-self: center;
              align-items: center;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              font-weight: 400;
              padding: 0 44px;
              color: #13feff;
              line-height: 112px;
            }
          }

          .patient {
            width: 100%;
            padding: 24px 30px;
            margin-top: 64px;
            background: linear-gradient(90deg, rgba(238, 238, 238, 0.4) 0%, rgba(216, 216, 216, 0) 100%);
            border-radius: 24px;
            display: flex;
            box-sizing: border-box;
            flex-direction: column;

            .patient-info {
              font-size: 54px;
              line-height: 84px;
              display: flex;
              text-align: left;

              .title {
                display: flex;
                flex-shrink: 0;
                color: #ffffff;
              }

              .wait {
                width: calc(100% - 185px);

                .wait-inside {
                  width: 100%;

                  li {
                    font-size: 54px;
                    line-height: 84px;
                    color: rgba(255, 254, 10, 1);
                    @include singleEllipsis(100%)
                  }
                }
              }

              .progress {
                width: 100%;
                color: #13feff;
                @include singleEllipsis(100%)
              }
            }
          }
        }

        //候诊人数为2
        .patient-2 {
          .doc {
            margin-top: 0 !important;
          }

          .patient {
            margin-top: 20px !important;
          }
        }
      }

      .divider {
        margin: 120px 0;
        height: 32px;
        background: url('~@/components/assets/images/design/line-bg.png') no-repeat center center;
        background-size: contain;
      }

      .first {
        padding-top: 100px;
      }

      .second {
        flex-direction: row-reverse;

        .picture {
          margin-right: 0;
        }

        .left {
          margin-right: 30px;

          .patient {
            background: linear-gradient(90deg, rgba(216, 216, 216, 0) 0%, rgba(238, 238, 238, 0.4) 100%);
            text-align: right;
          }

          .doc {
            .title {
              margin-right: 20px;
            }

            .content {
              margin-right: 0;
            }
          }
        }
      }
    }
  }
}
</style>